/**
 * Handles the class DBProductTest.
 * 
 * Author: Group 3
 * Date: 07-04-2014 Version: 1.0
 */
package testlayer;

import static org.junit.Assert.*;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;


import modellayer.Product;


import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import dblayer.DBProduct;
import dblayer.DBConnection;


public class DBProductTest {
	
	
	
	private static DBProduct testProductDB = new DBProduct();
	private static Product testProductInsert = new Product();
	private static DBConnection testConnection = DBConnection.getInstance();

	@BeforeClass
	/**
	 * 
	 * @throws SQLException
	 * @throws UnsupportedEncodingException
	 * @throws FileNotFoundException
	 */
	public static void setUp() throws SQLException, UnsupportedEncodingException, FileNotFoundException {
		
		testConnection.resetDatabase();
		
		testProductInsert.setName("Vests");
		testProductInsert.setPurchasePrice(50);
		testProductInsert.setSalesPrice(100);
		testProductInsert.setRentPrice(10);
		testProductInsert.setCountryOfOrigin("Danmark");
		testProductInsert.setMinStock(3);
		testProductInsert.setInStock(8);
		testProductInsert.setSupplierID(1);
		testProductInsert.setProductType("Clothing");
			}

	@AfterClass
	public static void tearDown() throws SQLException, FileNotFoundException {
		
		testConnection.resetDatabase();
	}

	/**
	 * Tests if you can get all the products in the database
	 */
	@Test
	public void testGetAllProducts() {
		assertNotNull(testProductDB.getAllProducts(true));
	}

	/**
	 * Tests if you can select a single product in the database
	 * @throws SQLException
	 */
	@Test
	public void testSelectSingleProduct() throws SQLException {
		assertNotNull(testProductDB.selectSingleProduct(1, true));
	}

	/**
	 * Tests if you can insert a product into the database
	 * @throws SQLException
	 */
	@Test
	public void testInsertProduct() throws SQLException {
		assertNotSame(-1, testProductDB.insertProduct(testProductInsert));
	}

	/**
	 * Tests if you can update a product in the database
	 * @throws SQLException
	 */
	@Test
	public void testUpdateProduct() throws SQLException {
		testProductDB.selectSingleProduct(1, true).setName("Pistol");
		assertNotSame(-1, testProductDB.updateProduct(testProductDB.selectSingleProduct(1, true)));
	}

	/**
	 * Tests if you can delete a product in the database
	 * @throws SQLException
	 */
	@Test
	public void testDeleteProduct() throws SQLException {
		
		assertNotSame(-1, testProductDB.deleteProduct(testProductDB.selectSingleProduct(1, true)));

}
	
}
